﻿using System;
using personalplaner.business.common;
using personalplaner.dataprovider.common;
using personalplaner.common;
using personalplaner.dataprovider;
using personalplaner.business.enums;

namespace personalplaner.servicemodel.common
{
    #region Interface

    public interface IDatabaseConectionHelper
    {

        bool TestConnection();

        bool TestConnection(string database);

        LoginData LoginData { get; set; }

        /// <summary>
        /// Checks and compares the database version with the api version
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        enVersionCompare CompareDatabaseVersion(DatabaseSettings settings, Version version);

        /// <summary>
        /// Gets the version of the personalplaner database
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        string GetDatabaseVersion(DatabaseSettings settings);
    }

    #endregion

    public class DatabaseConectionHelper : IDatabaseConectionHelper
    {
        public bool TestConnection()
        {
			try
			{
				var login = LoginData.Deserialize();

				if (login == null)
					return false;

				_logindata = login;

				DataServices.DatabaseService.TestDatabaseConnection(login);
			}
			catch (Exception)
			{
				return false;
			}

			return true;
		}

        public bool TestConnection(string database)
        {
            try
            {
                var login = LoginData.Deserialize();

                if (login == null)
                    return false;

                if (string.IsNullOrEmpty(database))
                    return false;

                login.Database = database;
                _logindata = login;

                DataServices.DatabaseService.TestDatabaseConnection(login);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

		LoginData _logindata;
        public LoginData LoginData
        {
            get
            {
				if (_logindata == null)
				{
					_logindata = LoginData.Deserialize();
					if (_logindata == null)
						_logindata = new LoginData();
				}
				return _logindata;
			}
            set
            {
				_logindata = value;
			}
		}


		/// <summary>
		/// Checks and compares the database version with the api version
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="version"></param>
		/// <returns></returns>
        public enVersionCompare CompareDatabaseVersion(DatabaseSettings settings, Version version)
        {
			using (new WaitCursor())
			{
				try
				{
					return DataServices.DatabaseService.CompareDatabaseVersion(settings, version);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return enVersionCompare.NoVersion;
			}
		}

		/// <summary>
		/// Gets the version of the personalplaner database
		/// </summary>
		/// <param name="settings"></param>
		/// <returns></returns>
        public string GetDatabaseVersion(DatabaseSettings settings)
        {
			using (new WaitCursor())
			{
				try
				{
					return DataServices.DatabaseService.GetDatabaseVersion(settings);
				}
				catch (Exception)
				{
					//TODO: show a messagebox here with the error
				}

				return "0.0.0.0";
			}
		}
	}
}
